﻿Public Class Search_Form

    Public WhoSentForMe As String


    Private Sub Search_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search_Button.Click

        Dim SearchString As String
        Dim SQL As String

        SearchString = Search_TextBox.Text.Replace("'", "''")

        SQL = "Select distinct R.RID,  P.UPIN, P.Provider_Name as 'Provider', L.LA_Name as 'Local Authority', L.Region_Code, L.LA_Code, f.UPIN as Live " & _
                "from Providers as P " & _
                "left outer join (select distinct UPIN from Provider_Funding_Streams) f " & _
                "on (f.UPIN = p.UPIN), " & _
                "Relationships as R, Local_Authorities as L  " & _
                "where R.UPIN = P.UPIN " & _
                "and R.LA_Code = L.LA_Code  " & _
                "and (P.Provider_Name Like '%" & SearchString & "%' or P.UPIN Like '%" & SearchString & "%')"

        SearchResults_DataViewGrid.DataSource = LoadDataView(SQL)
        SearchResults_DataViewGrid.Columns(0).Visible = False
        SearchResults_DataViewGrid.Columns(4).Visible = False
        SearchResults_DataViewGrid.Columns(5).Visible = False
        SearchResults_DataViewGrid.Columns(6).Visible = False
        SearchResults_DataViewGrid.Refresh()

    End Sub


    Private Sub CloseButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseButton.Click
        If WhoSentForMe = "" Or WhoSentForMe = "Providers" Then
           
            Me.Close()

        Else
           
                'its being used as a dialog
                Me.DialogResult = Windows.Forms.DialogResult.Cancel

        End If
    End Sub

    Private Sub SearchResults_DataViewGrid_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles SearchResults_DataViewGrid.CellDoubleClick
        ' Check to see if a provider exists in the list, if not do nothing
        If SearchResults_DataViewGrid.CurrentRow.Index = -1 Then

        Else
            If WhoSentForMe = "" Or WhoSentForMe = "Providers" Then

                ShowLoadingForm()

                'Volumes Form Docking to Main Form
                VolumesForm.MdiParent = MainForm
                VolumesForm.Dock = DockStyle.Fill

                VolumesForm.CurrentProviderName(SearchResults_DataViewGrid.Item(2, SearchResults_DataViewGrid.CurrentRow.Index).Value, _
                                                SearchResults_DataViewGrid.Item(0, SearchResults_DataViewGrid.CurrentRow.Index).Value, _
                                                SearchResults_DataViewGrid.Item(5, SearchResults_DataViewGrid.CurrentRow.Index).Value, _
                                                SearchResults_DataViewGrid.Item(1, SearchResults_DataViewGrid.CurrentRow.Index).Value)


                VolumesForm.Show()
                HideLoadingForm()

                Me.Close()

            Else

                'its being used as a dialog
                Me.DialogResult = Windows.Forms.DialogResult.OK

            End If

        End If
    End Sub

    Private Sub SearchResults_DataViewGrid_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles SearchResults_DataViewGrid.CellPainting
        e.Paint(e.ClipBounds, (DataGridViewPaintParts.All))

        Try
            If IsDBNull(SearchResults_DataViewGrid.Item("Live", e.RowIndex).Value) Then

                e.CellStyle.ForeColor = Color.Salmon

            End If
        Catch ex As Exception

        End Try
    End Sub

End Class